import { RouteRecordRaw, createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
// import Layout from '@/views/layout/Layout'
import home from '@/views/home/index.vue'
import { isMicroApp } from '@/utils/micro'
import { dyncRoutes, USE_REMOTE_ROUTE_TABLE } from '@/utils/routes'
import { getRouters } from '@/api/menu'
import navData from '@/i18n/nav.json'
import { store } from '@/store'

const packageName = require('../../package.json').name

// 公共路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    // hidden: true
  },
  {
    name: 'home',
    path: '/',
    component: home,
    redirect: 'homePage',
    // hiddenParent: true,
    children: [],
  },
  {
    path: '/outLogin',
    component: () => import('@/views/outLogin/index.vue'),
    // hidden: true
  },
  {
    path: '/outExamine',
    component: () => import('@/views/outExamine/index.vue'),
    // hidden: true
  },
]
if (localStorage.getItem('routes')) {
  const routes = JSON.parse(localStorage.getItem('routes') as string)
  console.log(routes, '路由缓存')
  const accessedRoutes = dyncRoutes(JSON.parse(JSON.stringify(routes)))
  constantRoutes[1].children = accessedRoutes
}
console.log(constantRoutes, '生成路由')

const baseUrl = isMicroApp() ? `/${packageName}` : '/'
export default createRouter({
  // mode: 'history', // 去掉url中的#
  history: createWebHashHistory(baseUrl),
  // scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes,
})

// export const router = USE_REMOTE_ROUTE_TABLE
//   ? new Promise(function (resolve) {
//       resolve(
//         useRouter(handleRouter() as any)
//       )
//     })
//   : new Promise(function (resolve) {
//       resolve(useRouter(dyncRoutes(navData['zh-cn'])))
//     })
// const handleRouter =async ()=>{
//   const res = await getRouters()
//   localStorage.setItem('routes', JSON.stringify(res.data.data))
//   console.log(JSON.parse(JSON.stringify(res.data.data)), 'getRouters')
//   const accessedRoutes = dyncRoutes(JSON.parse(JSON.stringify(res.data.data)))
//   constantRoutes[1].children = accessedRoutes
//   return constantRoutes
// }
// function useRouter(dyncRoutes: Array<any>) {
//   return createRouter({
//     history: createWebHistory(isMicroApp() ? '/' + packageName : '/'),
//     routes: [...dyncRoutes],
//   })
// }
